let win = window["huiyouyuan"];
export function query(name, source) {
  /* eslint-disable */
  name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  let url = source || location.search;
  let regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(url);
  return results == null ? "" : decodeURIComponent(results[1]);
}

export function is_ios() {
  if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
    return true;
  } else {
    return false;
  }
}
// 封装native 各类方法
export function handle_native_skip(method,...url){
  if(win){
    console.log(method,url)
    win[method] && win[method](...url)
  }
}

export function is_weixin() {
  var ua = navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == "micromessenger") {
    return true;
  } else {
    return false;
  }
}

var weixin_wait = 2;
function weixin_time() {
  if (weixin_wait == 0) {
    $("#topiframe").remove();
    $("#zzbg").remove();
    weixin_wait = 2;
  } else {
    weixin_wait--;
    setTimeout(function() {
      weixin_time();
    }, 1000);
  }
}

export function downLoadApk(apk) {
  if (is_ios()) {
    window.location.href = "https://itunes.apple.com/cn/app/id1319811972";
    return;
  }
  if (is_weixin() == false) {
    if (!apk) {
      location.href = "https://file.cashtoutiao.com/apk/huitoutiao.apk";
    } else {
      location.href = "https://file.cashtoutiao.com/apk/" + apk + ".apk";
    }
  } else {
    $(
      "<div id='topiframe' style='position:fixed; _position:absolute; top:14px; z-index:20000; width:100%; height:75px; background:#fff; font:14px/25px 微软雅黑; color:#6a6a6a; padding:15px 0;'><div style='width:80%;float:right; margin-right:30px;'>微信请点击右上角 <span style='width:16px; height:16px; display:inline-block; background:url(http://huisuoping.com:80/resources/moneyLock_images/layerimg.png) no-repeat; vertical-align:middle; margin-top:-2px; margin-right:5px;'></span>, 使用“在浏览器中打开”, 即可下载体验。<s style='width:31px; height:14px; background:url(http://huisuoping.com:80/resources/moneyLock_images/arrow.png) no-repeat; display:block; position:absolute; top:-14px; right:-2px;'></s></div></div><div id='zzbg' style='background-color:#000; width:100%; height:100%; left:0; top:0; filter:alpha(opacity=80); opacity:0.8; z-index:19999; position:fixed!important; position:absolute;'></div>"
    ).appendTo("body");
    weixin_time();
  }
}

export function getUserDetailInfo() {
  if (win) {
    let userInfo = win.getUserInfo && win.getUserInfo();
    let user = eval("(" + userInfo + ")");
    return user;
  }
  return {};
}

export function handle_native_method(para, ...arg) {
  if (win) {
    let info = win[para] && win[para](...arg);
    if (!!info) {
      return JSON.parse(info) ? JSON.parse(info) : eval("(" + info + ")");
    }
  }
  return {
    // userId: 10030,
    userId: 1000,
    // loginId:"217e80da5dcd430b92c505f69f24e843",
    loginId: "1",
    platform: 0,
    appVersion: "1.0.0.0",
    appCode: 1000,
    channel: "huiyouyuan",
    gender:"2",
  };
}
const utils = {
  query,
  is_ios,
  is_weixin,
  downLoadApk,
  getUserDetailInfo,
  handle_native_method,
  handle_native_skip
};

export default utils;
